package com.ruangong.GitClient.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.ruangong.GitClient.R;

public class AddTravelActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addtravel);

        final EditText editTextEventName = findViewById(R.id.editTextEventName);
        final EditText editTextNumberOfPeople = findViewById(R.id.editTextNumberOfPeople);
        Button buttonSave = findViewById(R.id.buttonSave);

        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入的旅游名称及人数
                String eventName = editTextEventName.getText().toString();
                String numberOfPeopleString = editTextNumberOfPeople.getText().toString();

                // 检查 numberOfPeopleString 是否为空
                if (!numberOfPeopleString.isEmpty()) {
                    try {
                        // 尝试将文本转换为整数
                        int numberOfPeople = Integer.parseInt(numberOfPeopleString);

                        // 创建一个新的 Intent 来传递数据给 TravelEventsListActivity
                        Intent resultIntent = new Intent();
                        resultIntent.putExtra("eventName", eventName);
                        resultIntent.putExtra("numberOfPeople", numberOfPeople);

                        // 设置结果代码为 RESULT_OK，并传递 Intent 给 TravelEventsListActivity
                        setResult(RESULT_OK, resultIntent);

                        // 关闭当前 Activity
                        finish();
                    } catch (NumberFormatException e) {
                        // 处理转换失败的情况，例如用户输入了非数字字符串
                        Toast.makeText(AddTravelActivity.this, "请输入有效的人数", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // 提示用户输入人数
                    Toast.makeText(AddTravelActivity.this, "请输入人数", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

